package com.algorithm.thePrimaryAlgorithm;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class ArraySulotionLC06 {
    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> result = new ArrayList<>();
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        // 找到长度最小的
        int right = 0;
        int left = 0;
        while (left < nums1.length && right < nums2.length) {
            if (nums1[left] == nums2[right]) {
                result.add(nums1[left]);
                left++;
                right++;
                continue;
            } else if (nums1[left] > nums2[right]) {
                right++;
            } else if (nums1[left] < nums2[right]) {
                left++;
            }
        }

        int[] r = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            r[i] = result.get(i);
        }

        return r;
    }

    @Test
    public void test() {
        int[] nums1 = {1, 2};
        int[] nums2 = {1, 1};
        intersect(nums1, nums2);
    }
}
